import { CopyControllerBase } from "./CopyControllerBase";
import { IPloughCopyController } from "../../../control/interfaces/IPloughCopyController";
import { PloughCopyIcon } from "../ploughCopy/PloughCopyIcon";
import { PloughCopyWindow } from "../ploughCopy/PloughCopyWindow";
import { PloughCopyResultView } from "../ploughCopy/PloughCopyResultView";
import { PloughCopyInfoPanel } from "../ploughCopy/PloughCopyInfoPanel";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SPloughInfo } from "../../../../../Message/Public/SPloughInfo";
import { Global } from "../../../../../com/gengine/global/Global";
import { SPloughCopyInfo } from "../../../../../Message/Public/SPloughCopyInfo";
import { SSeqPlayerItem } from "../../../../../Message/Public/SSeqPlayerItem";
import { DataEvent } from "../../../events/DataEvent";
import { GameProxy } from "../../../mvc/GameProxy";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
type int = number;
//class PloughCopyController
    
    export  class PloughCopyController extends CopyControllerBase implements IPloughCopyController
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */get ploughCopyIcon(): PloughCopyIcon
        {
            if (this._ploughCopyIcon == null) 
            {
                this._ploughCopyIcon = new PloughCopyIcon();
                this._ploughCopyIcon.updateIconInfo();
            }
            return this._ploughCopyIcon;
        }

        /* internal  */get ploughCopyWindow(): PloughCopyWindow
        {
            if (this._ploughCopyWindow == null) 
            {
                this._ploughCopyWindow = new PloughCopyWindow();
            }
            return this._ploughCopyWindow;
        }

        /* internal  */get ploughCopyResult(): PloughCopyResultView
        {
            if (this._ploughCopyResult == null) 
            {
                this._ploughCopyResult = new PloughCopyResultView();
            }
            return this._ploughCopyResult;
        }

        /* internal  */get ploughCopyInfoPanel(): PloughCopyInfoPanel
        {
            if (this._ploughCopyInfoPanel == null) 
            {
                this._ploughCopyInfoPanel = new PloughCopyInfoPanel();
            }
            return this._ploughCopyInfoPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPloughInfo, this.onPloughInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPloughCopyInfo, this.onPloughCopyInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPloughRewardItems, this.onPloughRewardItemsHandler);
            Dispatcher.addEventListener(EventName.PloughCopyEnter, this.onPloughCopyEnterHandler);
            Dispatcher.addEventListener(EventName.PloughCopyOpen, this.onPloughCopyOpenHandler);
            Dispatcher.addEventListener(EventName.PloughCopyLeft, this.onPloughCopyLeftHandler);
            return;
        }

        /* internal  */onPloughInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPloughInfo;
            if (loc1) 
            {
                cache.ploughCopy.ploughNum = loc1.num;
                cache.ploughCopy.ploughFloor = loc1.floor;
                if (loc1.num <= 0 && this.ploughCopyIcon.isHide || Global.isDebugModle) 
                {
                    this.ploughCopyIcon.show();
                }
            }
            return;
        }

        /* internal  */onPloughCopyInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPloughCopyInfo;
            if (loc1) 
            {
                cache.ploughCopy.killBossNum = loc1.killBossNum;
                cache.ploughCopy.totalBossNum = loc1.totalBossNum;
                cache.ploughCopy.curFloor = loc1.floor;
                cache.ploughCopy.leftTime = loc1.leftTime;
                cache.ploughCopy.useTime = loc1.useTime;
                cache.ploughCopy.finished = loc1.finished;
                this.ploughCopyInfoPanel.updatePloughInfos();
            }
            return;
        }

        /* internal  */onPloughRewardItemsHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqPlayerItem;
            var loc2=loc1.playerItem as Array<any>;
            if (loc2 && loc2.length > 0) 
            {
                this.ploughCopyResult.updateResult(true);
                this.ploughCopyResult.updateRewardList(loc2);
            }
            else 
            {
                this.ploughCopyResult.updateResult(false);
            }
            var loc3=(Global.stage.stageWidth - this.ploughCopyResult.width) / 2;
            var loc4=(Global.stage.stageHeight - this.ploughCopyResult.height) / 2;
            this.ploughCopyResult.show(loc3, loc4);
            return;
        }

        /* internal  */onPloughCopyEnterHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.enterPlough(loc1);
            return;
        }

        /* internal  */onPloughCopyOpenHandler(arg1: DataEvent): void
        {
            if (this.ploughCopyWindow.isHide) 
            {
                this.ploughCopyWindow.show();
            }
            return;
        }

        /* internal  */onPloughCopyLeftHandler(arg1: DataEvent): void
        {
            GameProxy.copy.leftGroup_async();
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            if (!this.ploughCopyWindow.isHide) 
            {
                this.ploughCopyWindow.hide();
            }
            if (!this.ploughCopyIcon.isHide && !Global.isDebugModle) 
            {
                this.ploughCopyIcon.hide();
            }
            this.ploughCopyInfoPanel.updatePosition();
            this.ploughCopyInfoPanel.show();
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            if (!this.ploughCopyInfoPanel.isHide) 
            {
                this.ploughCopyInfoPanel.hide();
            }
            return;
        }

        /* internal */ /* var */_ploughCopyIcon: PloughCopyIcon;

        /* internal */ /* var */_ploughCopyWindow: PloughCopyWindow;

        /* internal */ /* var */_ploughCopyInfoPanel: PloughCopyInfoPanel;

        /* internal */ /* var */_ploughCopyResult: PloughCopyResultView;
    }
